<?php

namespace app\api\controller;

use think\Db;

class Page extends Common
{
    function view()
    {
        $id = input('id/d');

        $data = Db::name('page')->cache(true)->find($id);

        preg_match_all("/{(.*?)}/", $data['body'], $content);

        $content[1] = array_unique($content[1]);

        foreach ($content[1] as $item) {
            if($item == 'TIXIAN_TIME'){
                $withdraw_config = Db::name('config_system')->where(['category'=>'withdrawtime'])->column('title, content');
                $time = $withdraw_config['withdraw_hour'] . ":" . $withdraw_config['withdraw_minute'];
                $data['body'] = str_replace('{'.$item.'}', $time, $data['body']);
            }else{
                $data['body'] = str_replace('{'.$item.'}', getenv($item), $data['body']);
            }
        }

        return json(['code' => 200, 'data' => $data]);
    }

    function index()
    {
        $id = input('id/d');

        $data = Db::name('page')->cache(true)->find($id);

        preg_match_all("/{(.*?)}/", $data['body'], $content);

        $content[1] = array_unique($content[1]);

        foreach ($content[1] as $item) {
            $data['body'] = str_replace('{'.$item.'}', getenv($item), $data['body']);
        }

        $this->assign('data', $data);
        return $this->fetch();
    }
}
